package odnoklassniki;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import randomwalk.PreferencesVector;
import simulation.CumulativeDistributionFunction;
import musictrackrecommendation.DatabaseDataset;


/**
 * For using JDBC with SQL Server you must enable TCP/IP connections  in Sql Server Configuration Tool and restart Sql Server
 */
public class OdnoklassnikiDataset extends DatabaseDataset {
	public static final String SQLSERVER_JDBC_DRIVER = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
    public static final String SQLSERVER_JDBC_CONNECTION_URL = "jdbc:sqlserver://YOUR-5F64BCCFD1";
    public static final String SQLSERVER_USER = "recommendation";
    public static final String SQLSERVER_PASSWORD = "recommendation";
    //names of dataset`s tables
    /**
	 * @uml.property  name="uSER_TABLE_NAME"
	 */
    public static final String USER_TABLE_NAME = "dbo.users";
    public static final String TRACK_TABLE_NAME = "dbo.track";
    public static final String ARTIST_TABLE_NAME = "dbo.artist";
    /**
	 * @uml.property  name="uSER_USER_TABLE_NAME"
	 */
    public static final String USER_USER_TABLE_NAME = "dbo.user_user";
    /**
	 * @uml.property  name="tRACK_TRACK_TABLE_NAME"
	 */
    public static final String TRACK_TRACK_TABLE_NAME = "dbo.track_track";
    /**
	 * @uml.property  name="uSER_TRACK_TABLE_NAME"
	 */
    public static final String USER_TRACK_TABLE_NAME = "dbo.user_track";
    public static final String TRAIN_TABLE_NAME = "dbo.small_user_track_train";
    public static final String QUIZ_TABLE_NAME = "dbo.small_user_track_quiz";
    public static final String USER_ARTIST_TABLE_NAME = "";
    //name of some attributes in tables
    public static final String ID_ATTRIBUTE = "id";
	
	@Override
	public String getDatabaseUser() {
		return SQLSERVER_USER;
	}

	@Override
	public String getJdbcConnectionUrl() {
		return SQLSERVER_JDBC_CONNECTION_URL;
	}

	@Override
	public String getJdbcDriverName() {
		return SQLSERVER_JDBC_DRIVER;
	}

	@Override
	public String getPassword() {
		return SQLSERVER_USER;
	}

	/**
	 * @return
	 * @uml.property  name="uSER_TRACK_TABLE_NAME"
	 */
	@Override
	public String getUserTrackTableName() {
		return USER_TRACK_TABLE_NAME;
	}

	/**
	 * @return
	 * @uml.property  name="uSER_USER_TABLE_NAME"
	 */
	@Override
	public String getUserUserTableName() {
		return USER_USER_TABLE_NAME;
	}

    @Override
    public PreferencesVector getRestartVector(int userId) {

        double[] userFriends = new double[getUsersMaximalId()+1];
        double[] userTracks = new double[getTracksMaximalId()+1];

        try {
            Class.forName(getJdbcDriverName());
            Connection con = DriverManager.getConnection(getJdbcConnectionUrl(), 
            		getDatabaseUser(),
            		getPassword());

            PreparedStatement pstmt;
            ResultSet rs;

            //select all user tracks
            pstmt = con.prepareStatement("select distinct track_id from dbo.user_track where user_id = ?");
            pstmt.setInt(1, userId);
            rs = pstmt.executeQuery();

            while (rs.next()) {
                userTracks[rs.getInt(1)] = 1.0;
            }
            
            rs.close();
            con.close();
        } 
        catch (Exception e) {
            e.getMessage();
        }
        
        PreferencesVector restartVector = 
        		new PreferencesVector(userFriends, userTracks, new double [0]);
        
        return restartVector;
    }

	@Override
	public int getTracksMaximalId() {
		return getMaximalId(TRACK_TABLE_NAME, ID_ATTRIBUTE);
	}

	@Override
	public int getUserTrackCount() {
		return  getItemsCount(USER_TRACK_TABLE_NAME);
	}

	@Override
	public int getUserUserCount() {
		return getItemsCount(USER_USER_TABLE_NAME);
	}
	
	@Override
	public int getTrackTrackCount() {
		return getItemsCount(TRACK_TRACK_TABLE_NAME);
	}

	@Override
	public int getUsersCount() {
		return getItemsCount(USER_TABLE_NAME);
	}

	@Override
	public int getUsersMaximalId() {
		return getMaximalId(USER_TABLE_NAME, ID_ATTRIBUTE);
	}

	@Override
	public String getUserIdAttribute() {
		return ID_ATTRIBUTE;
	}

	/**
	 * @return
	 * @uml.property  name="uSER_TABLE_NAME"
	 */
	@Override
	public String getUserTableName() {
		return USER_TABLE_NAME;
	}

	/**
	 * @return
	 * @uml.property  name="tRACK_TRACK_TABLE_NAME"
	 */
	@Override
	public String getTrackTrackTableName() {
		return TRACK_TRACK_TABLE_NAME;
	}

	@Override
	public String getUserTrackQuizName() {
		return QUIZ_TABLE_NAME;
	}

	@Override
	public String getUserTrackTrainName() {
		return TRAIN_TABLE_NAME;
	}

	@Override
	public CumulativeDistributionFunction getTrackSimilarItems(int trackId) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public CumulativeDistributionFunction getUserSimilarItems(int userId) {
		// TODO Auto-generated method stub
		return null;
	}
}
